﻿Imports System.Data.SqlClient
Imports PhDir.DAL
Imports PhDir.Model
Imports PhDir.Repository
Imports PhDir.Repository.DataReader
Imports System.Collections.Generic

Namespace Repository.Phone

    Public Class SearchPlainPhone
        Implements ISearch(Of Model.Phone, String)

        Public Function Search(plainPhoneNumber As String) As List(Of Model.Phone) _
            Implements ISearch(Of Model.Phone, String).Search

            Dim sqlDataReader As SqlDataReader        = Nothing
            Dim phoneReader   As PhoneReader          = New PhoneReader()
            Dim phones        As List(Of Model.Phone) = New List(Of Model.Phone)

            Using dbConnection = New DbConnection(DbContext.PhoneContext)
                dbConnection.AddParameter("@PhoneNumber", plainPhoneNumber)
                dbConnection.CommandText = StoredProceduresName.GetPlainPhone
                dbConnection.CommandType = CommandType.StoredProcedure
                sqlDataReader            = dbConnection.ExecuteReader()

                While sqlDataReader.Read()
                    Dim phone = phoneReader.Read(sqlDataReader)
                    phones.Add(phone)
                End While
            End Using

            Return phones

        End Function

    End Class

End Namespace
